<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="参数描述" prop="parameterDesc">
        <el-input
            v-model="queryParams.parameterDesc"
            placeholder="请输入参数描述"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数类型" prop="parameterType">
        <el-input
          v-model="queryParams.parameterType"
          placeholder="请输入参数类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:businessComponeParametersVersion:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:businessComponeParametersVersion:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:businessComponeParametersVersion:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="参数描述" align="center" prop="parameterDesc" />
      <el-table-column label="参数类型" align="center" prop="parameterType" :formatter="formatParameterType" />
      <el-table-column label="排序号" align="center" prop="sortNumber" />
      <el-table-column label="版本号" align="center" prop="version" />
      <el-table-column label="是否当前版本" align="center" prop="isCurrent" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:businessComponeParametersVersion:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:businessComponeParametersVersion:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改业务组件参数版本表对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="mainRef" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="参数描述" prop="parameterDesc">
          <el-input v-model="form.parameterDesc" placeholder="请输入参数描述" />
        </el-form-item>
        <el-form-item label="参数类型" prop="parameterType">
          <el-select
              v-model="form.parameterType"
              placeholder="请选择参数类型"
              clearable
              style="width: 240px"
          >
            <el-option
                v-for="item in dataTypeList"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="排序号" prop="sortNumber">
          <el-input v-model="form.sortNumber" placeholder="请输入排序号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BusinessComponeParametersVersion">
import { listBusinessComponeParametersVersion, getBusinessComponeParametersVersion, delBusinessComponeParametersVersion, addBusinessComponeParametersVersion, updateBusinessComponeParametersVersion } from "@/api/business/businesscomponeparametersversion";
import { listData } from "@/api/system/dict/data";
import {getRandomLong} from "@/utils/ruoyi";
const { proxy } = getCurrentInstance();

const mainList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dataTypeList = ref([])
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])
const queryDictParams= ref( {
  pageNum: 1,
  pageSize: 10,
  dictType: 'business_data_type',
  dictLabel: undefined,
  status: undefined
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    parameterId:null,
    enterpriseCode:null,
    componentCode:null,
    parameterCode:null,
    parameterName:null,
    parameterType:null,
    sortNumber:null,
    createBy:null,
    createTime:null,
    updateBy:null,
    updateTime:null,
    parameterDesc:null,
    version:null,
    isCurrent:null,
  },
  rules: {

  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询业务组件参数版本表列表 */
function getList() {
  loading.value = true;
  listBusinessComponeParametersVersion(queryParams.value).then(response => {
    mainList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    parameterId:null,
    enterpriseCode:null,
    componentCode:null,
    parameterCode:null,
    parameterName:null,
    parameterType:null,
    sortNumber:null,
    createBy:null,
    createTime:null,
    updateBy:null,
    updateTime:null,
    parameterDesc:null,
    version:null,
    isCurrent:null
  };
  proxy.resetForm("mainRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.parameterid);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加业务组件参数版本表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _parameterid = row.parameterId || ids.value
  //循环mainList查找parameterid等于row.parameterid的行
  for(let i=0;i<mainList.value.length;i++){
    if(mainList.value[i].parameterId==_parameterid){
      form.value = JSON.parse(JSON.stringify(mainList.value[i]));
      open.value = true;
      break;
    }
  }
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["mainRef"].validate(valid => {
    if (valid) {
      if (form.value.parameterId != null) {
        for (let i = 0; i < mainList.value.length; i++) {
          if (mainList.value[i].parameterId == form.value.parameterId) {
            // mainList.value[i] = form.value;
            mainList.value[i]=JSON.parse(JSON.stringify(form.value));

            open.value = false;
            // reset();
            break;
          }
        }
      }else{
        form.value.parameterId = getRandomLong();
        mainList.value.push(JSON.parse(JSON.stringify(form.value)));
        console.log(mainList.value);
        open.value = false;
        reset();
      }
      console.log('bsparametersversion:'+JSON.stringify(mainList.value));
      emit('update:modelValue', mainList.value)
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _parameterids = Array.isArray(row.parameterId)
      ? row.parameterId
      : [row.parameterId || ids.value].flat();
  proxy.$modal.confirm('是否确认删除参数编号为"' + _parameterids + '"的数据项？').then(function() {
    //删除指定行
    for(let i=mainList.value.length-1;i>=0;i--){
      for(let j=0;j<_parameterids.length;j++)
        if(mainList.value[i].parameterId == _parameterids[j]){
          mainList.value.splice(i,1);
        }
    }

    emit('update:modelValue', mainList.value)
  }).then(() => {
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}


function getDictList() {
  listData(queryDictParams.value).then(response => {
    dataTypeList.value = response.rows;
  });
}
/**
 * 翻译参数类型
 * @param row
 * @param column
 * @returns {*|string}
 */
function  formatParameterType(row, column){
  //debugger;
  // console.log('parameter_type:'+row.parameterType+","+JSON.stringify(dataTypeList.value))
  return dataTypeList.value.find(k => k.dictValue === row.parameterType)?.dictLabel ?? '';
}
getList();
getDictList();
</script>
